﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Web.UserControls.Tasks
{
    public partial class ucTaskHistory : System.Web.UI.UserControl
    {
        private string _controlname = "TaskHistory";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(Session["UserControlLoaded" + _controlname]))
            {
                // Initiate datalayer
                TireServiceDB db = new TireServiceDB();

                // Show tasklog
                LoadTaskLog(Convert.ToInt32(Session["TaskId" + _controlname]), db);
            }
        }

        protected void callbackTaskHistoryPanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            Session["TaskId" + _controlname] = Convert.ToInt32(e.Parameter);

            LoadUserControl(Convert.ToInt32(e.Parameter));
        }

        /// <summary>
        /// Public function for loading usercontrol
        /// </summary>
        public void LoadUserControl(int TaskId)
        {
            // Show usercontrol edit form
            pTaskHistory.Visible = true;

            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();

            // Load form data
            LoadUserControlData(db);


            if (TaskId != 0)
            {
                LoadTaskLog(TaskId, db);
            }
            else
            {
                // Error
                throw new ArgumentException("Opgave ikke fundet");
            }
        }

        /// <summary>
        /// Function that load form data for edit
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void LoadUserControlData(TireServiceDB db)
        {
            // TODO: If needed
        }

        /// <summary>
        /// Function that load task history and set edit form values
        /// </summary>
        /// <param name="TaskId">Id on task to load</param>
        /// <param name="db">Connection to database</param>
        private void LoadTaskLog(int TaskId, TireServiceDB db)
        {
            BindTaskLogs(TaskId, db);

            Session["UserControlLoaded" + _controlname] = true;
        }

        private void BindTaskLogs(int TaskId, TireServiceDB db)
        {
            gvTaskLog.DataSource = db.TaskLogs.Where(p => p.TaskId == TaskId).OrderBy(d => d.LogDate).ToList();
            gvTaskLog.DataBind();
        }

        /// <summary>
        /// Postback function that updates gridview with tasklogs
        /// </summary>
        protected void gvTaskLog_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Show tasklogs
            BindTaskLogs(Convert.ToInt32(Session["TaskId" + _controlname]), db);
        }
    }
}